layui.use(['table', 'form', 'vip_table', 'jquery', 'fms_util', 'element','laydate'], function () {
    // 操作对象
    var form = layui.form,
        table = layui.table,
        vipTable = layui.vip_table,
        fmsUtil = layui.fms_util,
        element = layui.element,
        laydate = layui.laydate,
        $ = layui.jquery;

    //日期选择
    laydate.render({
        elem:'#startTime',
        type: 'datetime',
        format:'yyyy-MM-dd HH',
        min:minDate()
    });
    function minDate(){
        var date = new Date(new Date().getTime() + 1 * 60 * 60 * 1000);
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
        console.log(currentdate)
        return currentdate;
    }

    var selsect={};
    selsect["operatorId"] = API.operatorId();
    selsect["userId"] = API.userId();

    // fmsUtil.doAjax({
    //     action: "",
    //     data: selsect,
    //     type: "post",
    //     success: function (result) {
    //         // console.log(result.data)
    //         if (result.isSuccess == true) {
    //             var list = result.data.list;
    //             for (var i = 0; i < list.length; i++) {
    //                 $('#deviceGroupId').append("<option value='" + list[i].id + "'>" + list[i].groupName + "</option>");
    //             }
    //             form.render('select');
    //             // datas = result.data;
    //         }
    //     }
    // });

    //表格加载
    var where = {};
    where[API.key.userId] = API.userId();
    where[API.key.systemSourceId] = API.systemSourceId();

    var tableIns = {};
    var tableIns1 = {};
    var changeCheckStatus = [];

    dataTable_add_left(where);
    function dataTable_add_left(where) {
        tableIns = vipTable.bdTable({
            table: table,
            elem: "#dataTable_left_add",
            id:"dataTable_left_add",
            limit: 6,
            cols: [[
                {type: 'numbers', title: '序',width:'5%', align: 'center'},
                {checkbox: true, sort: true,align:'center'},
                {field: 'code', title: '基站编号', align: 'center'},
                {field: 'name', title: '基站名称', align: 'center'}
            ]],
            data:[
                {code:1,name:2}
            ]
        });
    }

    function dataTable_right_add(datas) {
        tableIns1 = vipTable.bdTable({
            table: table,
            elem: "#dataTable_right_add",
            id:"dataTable_right_add",
            limit: 6,
            cols: [[
                {type: 'numbers', title: '序',width:'5%', align: 'center'},
                {checkbox: true, sort: true, align:'center'},
                {field: 'code', title: '基站编号', align: 'center'},
                {field: 'name', title: '基站名称', align: 'center'}
            ]],
            data: datas,
            done: function (res, curr, count) {
                // console.log(res);
            }
        });
    }
    dataTable_right_add([]);

    $(".arrow-right").click(function () {
        var checkStatus = table.checkStatus('dataTable_left_add').data;
        where['filterStationIds'] = [];
        if(checkStatus.length == 0){
            fmsUtil.msg("请选择基站列表的数据");
            return
        }
        for (var i = 0; i < checkStatus.length; i++) {
            where['filterStationIds'].push(checkStatus[i].id);
            changeCheckStatus.push(checkStatus[i]) ;
        }

        dataTable_right_add(changeCheckStatus);
        tableIns.reload({
            searchState: true,
            where: where
        });
    });

    $(".arrow-left").click(function () {
        var checkStatus = table.checkStatus('dataTable_right_add').data;
        where['filterStationIds'] = [];
        if(checkStatus.length == 0){
            fmsUtil.msg("请选择已选基站列表的数据");
            return
        }
        for (var i = 0; i < changeCheckStatus.length; i++) {
            for (var j = 0; j < checkStatus.length; j++) {
                if(changeCheckStatus[j].id === checkStatus[i].id){
                    changeCheckStatus.splice(i, 1);
                }else {
                    where['filterStationIds'].push(checkStatus[i].id);
                }
            }
        }

        dataTable_right_add(changeCheckStatus);
        tableIns.reload({
            searchState: true,
            where: where
        });
    });


    form.on('radio(executeType)', function (data) {
        if(data.value != '2'){
            $("#startTime").attr("disabled","disabled");
            $("#startTime").val('')
        }else {
            $("#startTime").removeAttr("disabled");
        }
    });

    form.on('submit(submit)', function (data) {
        if(changeCheckStatus.length == 0){
            fmsUtil.msg("请添加已选择的数据")
            return
        }
        var datas = data.field;
        if(datas.executeType == "2"){
            if(datas.preExecuteTime == ""){
                fmsUtil.msg("请选择定时执行时间")
                return
            }
        }
        datas.stationIds=[];
        for (var i=0; i<changeCheckStatus.length; i++) {
            datas.stationIds.push(changeCheckStatus[i].id)
        }
        updateState(datas);
    });

    //搜索

    //搜索功能
    form.on('submit(search)', function (data) {
        where.pageNo=1;
        var searchWhere = fmsUtil.filterObject($.extend(where, data.field));
        tableIns.reload({
            searchState: true,
            where: fmsUtil.filterObject(searchWhere)
        });
        return false;
    });

    table.on('tool(dataTable)', function (obj) {
        var layEvent = obj.event;
        var value = obj.value,//得到修改后的值
            data = obj.data,//得到所在行所有键值
            field = obj.field; //得到字段

        if (layEvent === 'edit') {
            active.edit(data.id);
        } else if (layEvent === 'dalete') {
            active.dalete(data.id);
        }
    });

    // 新增功能
    form.on('submit(add)', function (data) {
        active.add();
        return false;
    });

    var active = {
        add: function () {
            fmsUtil.replaceHref('alarm-setup-add.html');
        },
        //删除
        dalete: function () {
            parent.layer.confirm('是否确定删除？', function(index){
                fmsUtil.doAjax({
                    action: deleteAction[showIndex],
                    data: {ids:ids.join(','),systemSourceId:API.systemSourceId()},
                    load: false,
                    type: 'get',
                    success: function (result) {
                        if (result.isSuccess) {
                            fmsUtil.msg('删除成功',{callBack:function(){
                                    tableIns1.reload();
                                }});
                        }else{
                            fmsUtil.msg(result.description);
                        }
                    }
                });
                layer.close(index);
            });
        }
    };

    //批量删除按钮
    $('button.batchDelete').on('click',function(){
        parent.parent.layer.confirm('是否确定删除？', {icon: 3, title:'提示'}, function(index){
            var ids=[];
            for(var i=0;i<el.length;i++){
                var ind=el.eq(i).parents('tr').attr('data-index');
                ids.push(tableData[ind].id);
            }
            fmsUtil.doAjax({
                action: deleteAction[showIndex],
                data: {ids:ids.join(','),systemSourceId:API.systemSourceId()},
                load: false,
                type: 'get',
                success: function (result) {
                    if (result.isSuccess) {
                        fmsUtil.msg('删除成功',{callBack:function(){
                                tableIns1.reload();
                            }});
                    }else{
                        fmsUtil.msg(result.description);
                    }
                }
            });
            layer.close(index);
        });
        return false;
    });

    //导出按钮
    $('button.export').on('click',function(){
        parent.parent.layer.confirm('是否确定导出？', {icon: 3, title:'提示'}, function(index){
            fmsUtil.doAjax({
                action: exportAction[showIndex],
                data: {systemSourceId:API.systemSourceId()},
                load: false,
                type: 'get',
                success: function (result) {
                    console.log(result);
                    if (result.isSuccess) {
                        window.open(result.data);
                        /*fmsUtil.msg('导出成功',{callBack:function(){
                            tableIns1.reload();
                        }});*/
                    }else{
                        fmsUtil.msg(result.description);
                    }
                }
            });
            layer.close(index);
        });
        return false;
    });

    function updateState(data){
        data['operatorId']=API.operatorId();
        data['systemSourceId']=API.systemSourceId();
        console.log(data)
        fmsUtil.msg('操作成功',{callBack:function(){
                fmsUtil.href("html/alarm/alarm-setup.html");
            }
        });
    }

    $(".closeBox").click(function () {
        fmsUtil.replaceHref('alarm-sync.html');
    })
});